package com.isoft.farm.controller;

import com.isoft.farm.common.JsonResult;
import com.isoft.farm.entity.SysUser;
import com.isoft.farm.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    SysUserService sysUserService;

    @RequestMapping("doLogin")
    @ResponseBody
    public JsonResult doLogin(String username, String password){

        boolean flag=sysUserService.doLogin(username,password);
        if(flag){
            return new JsonResult();//登陆成功
        }else{
            return new JsonResult("NO");
        }

    }
    @RequestMapping("doReg")
    @ResponseBody
    public JsonResult doReg(SysUser sysUser){
        System.out.println(sysUser);
        boolean flag=sysUserService.doReg(sysUser);
        if(flag){
            return new JsonResult();//登陆成功
        }else{
            return new JsonResult("NO");
        }
    }
    @RequestMapping("/doFindPageObjects")
    @ResponseBody
    public JsonResult doFindPageObjects(String uesername,Integer pageCurrent){
        return new JsonResult(sysUserService.findPageObjects(uesername,pageCurrent));
    }
    @RequestMapping("/doValidById")
    @ResponseBody
    public JsonResult doValidById(Integer id,Integer valid){
        sysUserService.validById(id,valid,"admin");
        return new JsonResult();
    }
    @RequestMapping("/doSaveObject")
    @ResponseBody
    public JsonResult doSaveObject(SysUser sysUser,Integer[] roleIds){
        sysUserService.saveObject(sysUser,roleIds);
        return new JsonResult();
    }
    @RequestMapping("/doFindObjectById")
    @ResponseBody
    public JsonResult doFindObjectById(Integer id){
        return new JsonResult(sysUserService.findObjectById(id));
    }

    @RequestMapping("/doUpdateObject")
    @ResponseBody
    public JsonResult doUpdateObject(SysUser sysUser,Integer [] roleIds){
        sysUserService.updateObject(sysUser,roleIds);
        return new JsonResult();
    }
}
